const express = require('express');
const router = express.Router();
const db = require('../libs/db');
const fs = require('fs');

router.get('/e/:id', function (req, res, next) {
    res.setHeader('Content-Type', 'text/javascript');
    let f = fs.readFileSync('public/javascripts/e.js', 'utf-8');
    const id = req.params.id;
    const domain = req.headers.host;
    f = f.replace(/EBSYS_DOMAIN/g, domain);
    f = f.replace(/EBSYS_ID/g, id);
    res.send(f);
});

router.get('/r/:id', function (req, res, next) {
    res.render('e');
});

router.get('/t', function (req, res, next) {
    res.render('r');
});

router.get('/snippet/:id', function (req, res, next) {
    db.getSnippet(req.params.id).then(d => {
        res.send(d);
    });
});

module.exports = router;